// 数组
// 交集   arr为第二个数组, type表示两者都是对象,且为指定参数判断, firstType表示只是前者是对象, lastType表示只是后者是对象
// eslint-disable-next-line no-extend-native
Array.prototype.intersect = function (arr, type, firstType, lastType) {
  if (type) {
    return this.filter(item => {
      return arr.some(arrItem => arrItem[type] === item[type])
    })
  } else if (firstType) {
    return this.filter(item => {
      return arr.some(arrItem => arrItem === item[firstType])
    })
  } else if (lastType) {
    return this.filter(item => {
      return arr.some(arrItem => arrItem[lastType] === item)
    })
  } else {
    return this.filter(item => {
      return arr.indexOf(item)
    })
  }
}
// 差集
// eslint-disable-next-line no-extend-native
Array.prototype.minus = function (arr, type, firstType, lastType) {
  if (type) {
    return this.filter(item => {
      return !arr.some(arrItem => arrItem[type] === item[type])
    })
  } else if (firstType) {
    return this.filter(item => {
      return !arr.some(arrItem => arrItem === item[firstType])
    })
  } else if (lastType) {
    return this.filter(item => {
      return !arr.some(arrItem => arrItem[lastType] === item)
    })
  } else {
    return this.filter(item => {
      return arr.indexOf(item) === -1
    })
  }
}
// 去重
// eslint-disable-next-line no-extend-native
Array.prototype.objRemoveRepeat = function (id, name) {
  const needData = []
  this.forEach(item => {
    const idArr = (item[id] || '').split(/[，|,]/)
    const nameArr = (item[name] || '').split(/[，|,]/)
    idArr.forEach((idItem, index) => {
      needData.push({ ...item, [id]: idItem, [name]: nameArr[index] })
    })
  })
  const map = new Map()
  for (const item of needData) {
    map.set(item[id], item)
  }
  return [...map.values()]
}
// 对象的排序 id为对象中的标识，dis默认为fasle，表示升序
// eslint-disable-next-line no-extend-native
Array.prototype.newSort = function (id, dis = false) {
  this.sort((a, b) => {
    if (a[id] - b[id] === 0) {
      return 0
    } else {
      return dis ? b[id] - a[id] : a[id] - b[id]
    }
  })
  return this
}